package com.atguigu.config;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author lhh
 * @ClassName UserDetailsService
 * @date 2022/9/29 19:56
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

    @Reference
    private AdminService adminService;

    @Reference
    private PermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Admin admin = adminService.getByUserName(username);
        if(admin==null){
            throw new UsernameNotFoundException("用户名不存在！");
        }

        List<String> codeList = permissionService.findCodeListByAdminId(admin.getId());
        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        //登录成功，遍历账号拥有的权限
        for (String code : codeList) {
            if(!StringUtils.isEmpty(code)){
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(code);
                authorities.add(authority);
            }
        }
        return new User(username,admin.getPassword(), authorities);
    }
}
